package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private DishMapper  dishMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    /**
     * 新增套餐
     *   新增套餐本身
     *   新增套餐和菜品的关联关系
     *      这里需要注意 只是增加了两站表的关联关系 并没有增加新菜品本身
     * @param setmealDTO
     */
    @Transactional
    @Override
    public void saveWithDish(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //1. 新增套餐本身
        setmealMapper.insert(setmeal);
        //2. 新增套餐关联的菜品 一对多
        Long id = setmeal.getId();
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if(!CollectionUtils.isEmpty(setmealDishes)){
            for (SetmealDish setmealDish : setmealDishes) {
                setmealDish.setSetmealId(id);
            }
            setmealDishMapper.insertSetmealDish(setmealDishes);
        }
    }

    /**
     * 套餐分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        //开启分页
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        //执行查询
        Page<SetmealVO> pageResult = setmealMapper.pageQuery(setmealPageQueryDTO);
        //封装到PageResult
        long total = pageResult.getTotal();
        List<SetmealVO> records = pageResult.getResult();
        return new PageResult(total,records);
    }

    /**
     * 批量删除套餐 涉及（根据id查询套餐 根据id删除套餐 根据id删除关系表数据）
     * @param ids
     */
    @Override
    public void deleteBatch(List<Long> ids) {
        //先判断套餐的状态是否在起售中 若在起售中不能删除
        for (Long id : ids) {
            //根据套餐id查询套餐
            Setmeal setmeal = setmealMapper.getById(id);
            //判断套餐状态
            if(setmeal.getStatus() == StatusConstant.ENABLE){
                //起售中
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }

        //遍历ids执行删除
        for (Long id : ids) {
            //删除套餐本身
            setmealMapper.deleteById(id);
            //删除套餐菜品的关系数据
            setmealDishMapper.deleteBySetmealId(id);
        }
    }

    /**
     * 根据id查询套餐以及菜品关联信息 页面回显
     * @param id
     * @return
     */
    @Override
    public SetmealVO getByIdWithDish(Long id) {
        //由于SetmealVO里已经封装了SetmealDish 所以直接查询SetmealVO即可
        return setmealMapper.getByIdWithDish(id);
    }

    /**
     * 根据id删除套餐（嵌套菜品关联）
     */
    @Transactional
    @Override
    public void update(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //1. 修改套餐本身
        setmealMapper.update(setmeal);

        //2. 删除套餐和菜品的关联关系
        Long setmealId = setmealDTO.getId();
        setmealDishMapper.deleteBySetmealId(setmealId);
        //3. 重新插入套餐和菜品的关联关系
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if(!CollectionUtils.isEmpty(setmealDishes)){
            for (SetmealDish setmealDish : setmealDishes) {
                setmealDish.setSetmealId(setmealId);
            }
            setmealDishMapper.insertSetmealDish(setmealDishes);
        }
    }

    /**
     * 套餐起售停售
     */
    @Override
    public void startOrStop(Integer status, Long id) {
        //先判断套餐中是否包含停售的菜品 则不能起售
        if(status == StatusConstant.ENABLE){
            //查询套餐关联的菜品
            List<Dish> dishList =  dishMapper.getBySetmealId(id);
            //循环判断菜品是否停售
            if (!CollectionUtils.isEmpty(dishList)) {
                for (Dish dish : dishList) {
                    if(dish.getStatus() == StatusConstant.DISABLE){
                        //包含停售的菜品 则不能起售
                        throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                    }
                }
            }
        }
        Setmeal setmeal = Setmeal.builder()
                .id(id)
                .status(status)
                .build();
        setmealMapper.update(setmeal);
    }


    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
